package org.onvivals.common.utils;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * 获取IP地址工具类
 *
 * @author hupeng
 * @date 2020-04-30
 */
public final class IpUtil {

	private static final String UNKNOWN = "unknown";
	private static final String IPV6_LOCAL = "0:0:0:0:0:0:0:1";

	private IpUtil() {
		throw new AssertionError();
	}

	/**
	 * 获取请求用户的IP地址
	 *
	 * @return
	 */
	public static String getRequestIp() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		return getRequestIp(request);
	}

	/**
	 * 获取请求用户的IP地址
	 *
	 * @param request
	 * @return
	 */
	public static String getRequestIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}

		if (IPV6_LOCAL.equals(ip)) {
			ip = getLocalhostIp();
		}
		return ip;
	}

	public static String getLocalhostIp() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
		}
		return null;
	}

}
